package array;

public class GenerateMatrix {

    public int[][] generateMatrix(int n) {

        int[][] matrix = new int[n][n];
        int upper_bound = 0, lower_bound = n - 1;
        int left_bound = 0, right_bound = n - 1;
        // 需要填入矩阵的数字
        int num = 1;
        while (num <= n * n) {
            if (upper_bound <= lower_bound) {
                for (int i = left_bound; i <= right_bound; i++)
                    matrix[upper_bound][i] = num++;
                // 上边界下移
                upper_bound++;
            }
            if (left_bound <= right_bound) {
                for (int i = upper_bound; i <= lower_bound; i++)
                    matrix[i][right_bound] = num++;
                // 右边界左移
                right_bound--;
            }
            if (upper_bound <= lower_bound) {
                for (int i = right_bound; i >= left_bound; i--)
                    matrix[lower_bound][i] = num++;
                // 下边界上移
                lower_bound--;
            }
            if (left_bound <= right_bound) {
                for (int i = lower_bound; i >= upper_bound; i--)
                    matrix[i][left_bound] = num++;
                // 左边界右移
                left_bound++;
            }
        }
        return matrix;
    }
}
